<template>
<a-select :getPopupContainer="(target) => target.parentNode" :value="getValueSting" show-search placeholder="搜索查找" :options="option" @change="handleInput" />
</template>

<script>
import {
    axios
} from '@/utils/request'

export default {
    name: 'JDictSelectRequest',
    props: {
        type: String,
        url: String,
        placeholder: String,
        triggerChange: Boolean,
        value: [String, Number],
        zplimit: {
            type: Object,
            default: () => {
                return {
                    key: false,
                }
            }
        },
    },
    data() {
        return {
            tagType: '',
            datas: '',
            userInfo: [],
            filterOption: (input, option) => {
                return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
            },
            option: [],
        }
    },
    computed: {
        getValueSting() {
            return this.value != null ? this.value.toString() : null
        },
    },
    mounted() {
        this.sendData();
    },
    methods: {
        sendRequest() {
            return axios.get(this.url)
        },
        sendData() {
            let self = this;
            //通过zplimit过滤出员工姓名
            if (self.zplimit.key) {
                (async function () {
                    self.option = [];
                    let result = await self.sendRequest();
                    self.userInfo = result.result.records;
                    for (var elem of result.result.records) {
                        var obj = {};
                        obj.value = elem.realname;
                        obj.label = elem.realname;
                        self.option.push(obj);
                    }
                })();
            } else {
                (async function () {
                    self.option = [];
                    let result = await self.sendRequest();
                    result.result.records.forEach((elem, index) => {
                        var obj = {};
                        obj.value = elem.odd;
                        obj.label = elem.odd;
                        self.$set(self.option, index, obj)
                    })
                })();
            }
        },
        handleInput(value) {
            this.$emit('change', value);
            if (this.userInfo.length > 0) {
                let user = this.userInfo.filter(elem => {
                    if (elem.realname == value) {
                        return elem
                    }
                })[0];
                this.$emit('userId', user.id);
            }
        }
    },
}
</script>

<style scoped>
</style>
